﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using WorkStation.Frameworks.Core.Json;

namespace WorkStation.Frameworks.Core
{
    /// <summary>
    /// * Object的静态扩展类
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// * 克隆一个对象
        /// </summary>
        /// <typeparam name="T">克隆类型</typeparam>
        /// <param name="value">被克隆对象</param>
        /// <returns></returns>
        public static T Clone<T>(this T value)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, value);

                    stream.Seek(0, SeekOrigin.Begin);
                    
                    T res = (T)formatter.Deserialize(stream);

                    return res;
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// 将对象转换为字节数组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this object input) 
        {
            using (MemoryStream stream = new MemoryStream()) 
            {
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();

                    formatter.Serialize(stream, input);

                    return stream.ToArray();
                }
                finally 
                {
                    if (stream != null) 
                    {
                        stream.Close();
                    }
                }
            }
        }

        /// <summary>
        /// * 转换对象为JSON数据
        /// </summary>
        /// <param name="value">被转换对象</param>
        /// <returns></returns>
        public static string ToJson(this object value)
        {
            return JsonHelper.ToJson(value);
        }

        /// <summary>
        /// * 继续的动作
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="value">继续的作用对象</param>
        /// <param name="action">动作</param>
        /// <returns></returns>
        public static T Continue<T>(this T value, Action<T> action)
        {
            if (value == null) { return value; }
            
            action(value);

            return value;
        }

        /// <summary>
        /// * 转换当前输入
        /// </summary>
        /// <typeparam name="TInput">输入</typeparam>
        /// <typeparam name="TOutput">输出</typeparam>
        /// <param name="value">输入对象</param>
        /// <param name="func">转换的委托</param>
        /// <param name="nullHandle">输入值为空时调用的委托</param>
        /// <returns></returns>
        public static TOutput Change<TInput, TOutput>(this TInput value, Func<TInput, TOutput> func, Func<TOutput> nullHandle = null) 
        {
            if (value == null) 
            {
                if (nullHandle != null)
                {
                    return nullHandle(); 
                }
                else
                {
                    return default(TOutput);
                }
            }

            return func(value);
        }
    }
}
